<?php
    $time = microtime();

    require '../../conf/lock.php';

    $parentesco = new acadParentescoRecord();
	$lib = new Lib();
	
	$tpl = new sistTemplate(APPTPLDIR.'/parentescoList.tpl.html');
    $tpl->addFile('TOPO', APPTPLDIR.'/topo.tpl.html');
    $tpl->addFile('MENULATERAL', APPTPLDIR.'/menuLateral.tpl.html');
    $tpl->addFile('RODAPE', APPTPLDIR.'/rodape.tpl.html');
    $tpl->IMAGEDIR = APPIMAGEDIR;
    $tpl->CSSDIR = APPCSSDIR;
    $tpl->JSDIR = APPJSDIR;
    $tpl->WEBROOT = APPWEBROOT;
    $tpl->SITETITLE = SITETITLE;
    $tpl->FAVICON = FAVICON;
    $tpl->ANIMATEDFAVICON = ANIMATEDFAVICON;
    $tpl->MEMORYUSAGE = number_format(intval(memory_get_usage()/1000), 0, ',', '.');
    $tpl->MEMORYPICK = number_format(intval(memory_get_peak_usage()/1000),0,',','.');    
	$tpl->CONTROLLER = $_SERVER["PHP_SELF"];
	$tpl->TITULOLISTAGEM = 'Parentesco';
	$tpl->TITULOPROCURAR = 'LOCALIZAR';
	
    $tpl->DICA = 'Lorem ipsum dolor sit amet, consectetur
        adipiscing elit. Duis sollicitudin ultrices
        erat vitae sodales. Duis pretium mollis
        risus, sed pellentesque diam accumsan
        et. Vivamus sapien lorem, ullamcorper
        n auctor non, lacinia et arcu.
        Maecenas condimentum tincidunt
        massa.';

    if(isset($_GET['orderBy']) and isset($_GET['sort']))
    {
        $ordCampo = $_GET['orderBy'];
		
		switch($_GET['orderBy'])
        {
            case 'cod':
            {
                if($_GET['sort'] == 'ASC')
                {
                    $tpl->IMGORDEM1 = '<img src="'.APPIMAGEDIR.'/down.gif" />';
                    $tpl->IMGORDEM2 = '';
                    $tpl->SORT = 'DESC';
                }
                else
                {
                    $tpl->IMGORDEM1 = '<img src="'.APPIMAGEDIR.'/up.gif" />';
                    $tpl->IMGORDEM2 = '';
                    $tpl->SORT = 'ASC';
                }
                break;
            }
            case 'nome':
            {
                if($_GET['sort'] == 'ASC')
                {
                    $tpl->IMGORDEM1 = '';
                    $tpl->IMGORDEM2 = '<img src="'.APPIMAGEDIR.'/down.gif" />';
                    $tpl->SORT = 'DESC';
                }
                else
                {
                    $tpl->IMGORDEM1 = '';
                    $tpl->IMGORDEM2 = '<img src="'.APPIMAGEDIR.'/up.gif" />';
                    $tpl->SORT = 'ASC';
                }
                break;
            }
        }
    }
    else
    {
        $ordCampo = 'nome';
		
		$tpl->IMGORDEM1 = '';
        $tpl->IMGORDEM2 = '<img src="'.APPIMAGEDIR.'/down.gif" />';
        $tpl->SORT = 'DESC';
    }
	 
	if(empty($_GET['pesquisa']))
	{ 
		$parentescos = $parentesco->listarParentesco($ordCampo,$tpl->SORT);
	} else
	{ 
		$texto = $lib->formatarString($_GET['pesquisa']);
		
		$parentescos = $parentesco->getParentesco($texto,$ordCampo,$tpl->SORT);
	}
	
	$totalParentesco = count($parentescos['COD']);
	
	$tpl->TOTAL = $totalParentesco;

	if(($totalParentesco > 0) and ($totalParentesco > 1))
	{
		$tpl->LEGENDA = ' parentescos encontrados.';
	} else
	{
		$tpl->LEGENDA = ' parentesco encontrado.';
	}
	
	for($i = 1; $i <= $totalParentesco; $i++)
    {
        if($i % 2 != 0)
        {
            $tpl->CLASS = '';
        }
        else
        {
            $tpl->CLASS = 'class="odd"';
        }

		$tpl->COD = $parentescos['COD'][$i];
        $tpl->NOME = $lib->formatarNome($parentescos['NOME'][$i]);
		$tpl->EDITAR = 'parentescoEdit.php?codParentesco='.$parentescos['COD'][$i];
        $tpl->block('BLOCK_LISTAGEM');
    }

    $tpl->TIME = number_format((microtime() - $time),3,',','.');
    $tpl->show();
?>